//
//  NYHomeEntryCell.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2018/1/4.
//  Copyright © 2018年 niuwaketang. All rights reserved.
//

#import "NYHomeEntryCell.h"

@interface NYHomeEntryCell ()

@property (nonatomic, strong) UIImageView *courseImgView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) TTTAttributedLabel *msgLabel;
@property (nonatomic, strong) UIImageView *lineImgView;

@property(nonatomic, strong) QMUIFloatLayoutView *floatLayoutView;

@end

@implementation NYHomeEntryCell


//MARK :Nimbus
- (BOOL)shouldUpdateCellWithObject:(NYHomeEntryModel *)object {
    
    if (![object isKindOfClass:[NYHomeEntryModel class]]) {
        return false;
    }
    [self updateContentWithModel:object];
    return true;
}
+ (CGFloat)heightForObject:(id)object identifier:(NSString *)identifier atIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView {
    
//    tableView.
    return 105.f;
}
- (void)setupView {
    [super setupView];
    
    //布局
    self.accessoryType = UITableViewCellAccessoryNone;
    [self.contentView addSubview:self.courseImgView];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.msgLabel];
    [self.contentView addSubview:self.lineImgView];
    
    [self.lineImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.contentView).offset(15);
        make.right.equalTo(self.contentView);
        make.bottom.equalTo(self.contentView);
        make.height.mas_equalTo(1);
    }];
}
- (void)updateContent {
    [super updateContent];
}
#pragma mark - Target Methods

#pragma mark - Public Methods

#pragma mark - Private Method
- (void)updateContentWithModel:(NYHomeEntryModel *)model {
    
    TTVIEW_RELEASE_SAFELY(_floatLayoutView);
    [self.contentView addSubview:self.floatLayoutView];

    //更新显示数据
    [_courseImgView sd_setImageWithURL:[NSURL URLWithString:model.cover]];
    [_titleLabel setText:model.title];
    
    NSString *msgString = [NSString stringWithFormat:@"共%ld节，已有%ld名学生学习",model.course_num, model.study_num];
    
    NSMutableAttributedString *hintString=[[NSMutableAttributedString alloc] initWithString:msgString];

    NSRange range0 = [[hintString string]rangeOfString:@"共"];
    NSRange range1 = [[hintString string]rangeOfString:[NSString stringWithFormat:@"%ld",model.course_num]];
    NSRange range2 = [[hintString string]rangeOfString:@"节，已有"];
    NSRange range3 =[[hintString string]rangeOfString:[NSString stringWithFormat:@"%ld",model.study_num]];
    NSRange range4 = [[hintString string]rangeOfString:@"名学生学习"];

    [hintString addAttribute:NSForegroundColorAttributeName value:UIColorMakeWithHex(@"#999999") range:range0];
    [hintString addAttribute:NSForegroundColorAttributeName value:UIColorMakeWithHex(@"#FF6F40") range:range1];
    [hintString addAttribute:NSForegroundColorAttributeName value:UIColorMakeWithHex(@"#999999") range:range2];
    [hintString addAttribute:NSForegroundColorAttributeName value:UIColorMakeWithHex(@"#FF6F40") range:range3];
    [hintString addAttribute:NSForegroundColorAttributeName value:UIColorMakeWithHex(@"#999999") range:range4];


    [_msgLabel setText:hintString];
    
    NSArray<NSString *> *suggestions = [model.course_type componentsSeparatedByString:@"|"];
    for (NSInteger i = 0; i < suggestions.count; i++) {
        QMUIGhostButton *button = [[QMUIGhostButton alloc] init];
        button.ghostColor = UIColorMakeWithHex(@"#D5D5D5");;
        [button setTitle:suggestions[i] forState:UIControlStateNormal];
        button.titleLabel.font = UIFontMake(10);
        button.contentEdgeInsets = UIEdgeInsetsMake(5, 10, 5, 10);
        [button setTitleColor:UIColorMakeWithHex(@"#D0D0D0") forState:0];
        [self.floatLayoutView addSubview:button];
    }

    CGFloat contentWidth = SCREEN_WIDTH - 140;
    CGSize floatLayoutViewSize = [self.floatLayoutView sizeThatFits:CGSizeMake(contentWidth, CGFLOAT_MAX)];
    self.floatLayoutView.frame = CGRectMake(130, CGRectGetMaxY(_msgLabel.frame) + 10, contentWidth, floatLayoutViewSize.height);
}
#pragma mark - External Delegate

#pragma mark - Setter Getter Methods
- (UIImageView *)courseImgView {
    if (!_courseImgView) {
        _courseImgView = [[UIImageView alloc] initWithFrame:CGRectMake(15.f, 15, 111.f, 75.f)];
        [_courseImgView setBackgroundColor:UIColorMakeWithHex(@"#E5E5E5")];
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_courseImgView.bounds cornerRadius:3.f];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
        maskLayer.frame = _courseImgView.bounds;
        maskLayer.path = maskPath.CGPath;
        _courseImgView.layer.mask = maskLayer;
    }
    return _courseImgView;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.courseImgView.frame) + 14.f, 20.f,SCREEN_WIDTH -CGRectGetMaxX(self.courseImgView.frame) - 29.f , 16.f)];
        _titleLabel.font = UIFontMake(15.f);
        _titleLabel.textColor = UIColorMakeWithHex(@"#333333");
    }
    return _titleLabel;
}
- (TTTAttributedLabel *)msgLabel {
    if (!_msgLabel) {
        _msgLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.courseImgView.frame) + 15.f, 45.f, SCREEN_WIDTH -CGRectGetMaxX(self.courseImgView.frame) - 30.f, 12.f)];
        _msgLabel.font = WGSystem12Font;
        _msgLabel.textColor = UIColorMakeWithHex(@"#999999");
        _msgLabel.enabledTextCheckingTypes = NSTextCheckingTypePhoneNumber;
        _msgLabel.highlightedTextColor = UIColorMakeWithHex(@"#FF6F40");
    }
    return _msgLabel;
}
- (QMUIFloatLayoutView *)floatLayoutView {
    if (!_floatLayoutView) {
        _floatLayoutView = [[QMUIFloatLayoutView alloc] init];
        _floatLayoutView.padding = UIEdgeInsetsMake(5, 10, 5, 10);
        _floatLayoutView.itemMargins = UIEdgeInsetsMake(0, 0, 10, 10);
        _floatLayoutView.minimumItemSize = CGSizeZero;
        _floatLayoutView.maximumItemSize = CGSizeMake(SCREEN_WIDTH - 140, 30);
        _floatLayoutView.centerFillLineView = false;
    }
    return _floatLayoutView;
}
- (UIImageView *)lineImgView {
    if (!_lineImgView) {
        _lineImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 15, 1)];
        //画一条虚线
        UIGraphicsBeginImageContext(_lineImgView.frame.size);   //开始画线
        [_lineImgView.image drawInRect:CGRectMake(0, 0, CGRectGetWidth(_lineImgView.frame), CGRectGetHeight(_lineImgView.frame))];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);  //设置线条终点形状
        const CGFloat lengths[] = {2,2};
        CGContextRef line = UIGraphicsGetCurrentContext();
        CGContextSetStrokeColorWithColor(line, [UIColor lightGrayColor].CGColor);
        CGContextSetLineDash(line, 0, lengths, 1);  //画虚线
        CGContextMoveToPoint(line, 0.0, 0.0);    //开始画线
        CGContextAddLineToPoint(line, _lineImgView.bounds.size.width, 0.0);
        CGContextStrokePath(line);
        _lineImgView.image = UIGraphicsGetImageFromCurrentImageContext();
    }
    return _lineImgView;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end
